package com.tedu.egmvc.controller;

import com.tedu.egmvc.mapper.AddressMapper;
import com.tedu.egmvc.pojo.dto.AddressDTO;
import com.tedu.egmvc.pojo.entity.Address;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Api(tags = "地址管理模块")
@Controller
public class AddressController {
    @Autowired
    private AddressMapper addressMapper;

    //@RequestMapping("/address/insert")

    @ApiOperation(value = "添加地址")
    @PostMapping("/address/insert")
    @ResponseBody
    public String insertAddress(AddressDTO addressDTO) {
        Address address = new Address();
        System.out.println(addressDTO);
        BeanUtils.copyProperties(addressDTO,address);
        System.out.println(address);
        int num=addressMapper.InsertAddress(address);
        return num>0?"success":"fail";
    }

   // @RequestMapping("/address/select")
    @ApiOperation(value = "查询地址")
    @GetMapping("/address/select")
    @ResponseBody
    public List<Address> selectAddress() {

        List<Address> list=addressMapper.SelectAllAddress();
        return list;

    }

//    @RequestMapping("/address/update")
    @ApiOperation(value = "更新地址")
    @PutMapping("/address/update")
    @ResponseBody
    public String updateAddress(AddressDTO addressDTO) {
        Address address = new Address();
        BeanUtils.copyProperties(addressDTO,address);
        int num=addressMapper.UpdateAddress(address);
        return num>0?"success":"fail";
    }

    //    @RequestMapping("/address/delete")
    @ApiOperation(value = "删除地址")
    @ApiImplicitParam(name = "id",value = "删除地址Id",required = true,example = "1")
    @DeleteMapping("/address/delete/{id}")
    @ResponseBody
    public String deleteAddress(@PathVariable int id) {
        int num=addressMapper.DeleteAddress( id);
        return num>0?"success":"fail";
    }

}
